from typing import List


class Solution:
    def sortArrayByParityII(self, A: List[int]) -> List[int]:
        A.sort()  # 先对数组进行排序
        B = []
        C = []
        for i in range(len(A)):  # 先分开奇数数组和偶数数组,记录的是索引值
            if i % 2 and A[i] % 2 == 0:
                B.append(i)
            elif i % 2 == 0 and A[i] % 2:
                C.append(i)

        for i in range(len(B)):  # 通过索引值交换
            A[B[i]], A[C[i]] = A[C[i]], A[B[i]]
        return A


# 输入：
l01 = [4, 2, 5, 7]
solution = Solution()
# 输出：[4, 5, 2, 7], [4,7,2,5]，[2,5,4,7]，[2,7,4,5] 也会被接受。
print(solution.sortArrayByParityII(l01))
